import $ from 'jquery'

export const qianchengService = {
    /*
    * 采集的信息
    *  「
    *
    *   教育经历、工作经历
    * 姓名、手机号、邮箱、性别、年龄、学历、城市、公司、职位、学校、专业、行业、标签、工作年限、期望薪资
    *
    * 」
    * */
    // 采集开始
    collection() {
        console.log('*** 51job ***');


        function setContentMap () {
            let elements = {}
            $('#divInfo .box').each(function () {
                let el = $(this)
                let text = el.find('.plate1').text()
                if (text.match(/目前年收入：/)) {
                    elements.salary = el
                } else if (text.match(/个人信息/)) {
                    elements.info = el
                } else if  (text.match(/求职意向/)) {
                    elements.mind = el
                } else if  (text.match(/工作经验/)) {
                    elements.work = el
                } else if  (text.match(/项目经验/)) {
                    elements.project = el
                } else if  (text.match(/教育经历/)) {
                    elements.eduction = el
                } else if  (text.match(/技能特长/)) {
                    elements.skill = el
                } else if  (text.match(/附加信息/)) {
                    elements.addition = el
                }
            })
            return elements
        }

        let elements = setContentMap()

        let base = getBasicInfo()
        let desired = getDesired()
        let eduction = getEducation()
        let work = getWorkInfo()
        let pro = getProInfo()
        let evaluation = getEvaluation()
        let other = getOther()
        let htmlFile = $util.htmlToString(document) // 获取源html代码
        // 基本信息
        function getBasicInfo() {
            // 姓名
            let name = $util.getChildText($('#tdseekname')).trim()
            // 在职状态
            let status = $('.info_part1 ul li').eq(-1).text().trim()
            // 性别
            let sex = ''
            let tempInfo =  $('.rz_pass_icon')[0].currentSrc
            if (tempInfo.includes('/wemen')) {
                sex = '女'
            } else if (tempInfo.includes('/men')) {
                sex = '男'
            }

            // 生日 todo
            let birth = tempInfo.match(/\d+年\d+月\d*/)
            birth = birth ? birth[0] : ''
            birth = birth.replace(/[年月]/g ,'-')
            let birthDate = new Date(birth)
            birth = birthDate.getTime() || 0

            // 工作年限
            let workExperience = tempInfo.match(/(\d+)年工作经验/)
            workExperience = workExperience ? workExperience[1] : 0

            // 学历
            let qualifications = $('.box2 .tb2').eq(1).find('tbody > tr').eq(3).find('.txt2').text().trim()

            let age = $('.info_part1 ul li').eq(0).text().trim().replace('岁', '')

            // 当前所在城市
            let nowCity = $('.info_part1 ul li').eq(6).text().trim()

            // 手机号、邮箱
            let phone =""
            let email = ""
            $('.info_part2 ul li').each(function(i,e){
                let str1 = $(e).text().trim();
                if(str1.length===11 || str1.length===12){
                    phone = str1;
                }else if(str1.includes('@')){
                    email= str1
                }
            });

            // 婚姻状态
            let maritalStatus = ''
            if (elements.info) {
                elements.info.find('.tba > table>tbody>tr>td').each(function () {
                    let key = $(this).find('.keys').text()
                    if (key.match(/婚姻状况/)) {
                        let marriage = $(this).find('.txt2').text()
                        if (marriage.includes('已婚')) {
                            maritalStatus = '已婚'
                        } else if (marriage.includes('未婚')) {
                            maritalStatus = '未婚'
                        }
                    }
                })

            }

            // 当前薪资 当前行业
            let currentSalary = '',currentIndustry = $('.box2 .tb2').eq(0).find('tbody > tr').eq(3).find('.txt2').text().trim()
            let currentYearlySalary = ''
            if (elements.salary) {
                currentSalary = elements.salary.find('.plate1').text().replace(/\s/g, '')
                // currentYearlySalary =  currentSalary.match(/\d+/)
                // currentYearlySalary = currentYearlySalary ? currentYearlySalary[0] : 0
                // if (currentSalary.match(/万元/)) {
                //     currentYearlySalary = currentYearlySalary * 10000
                // }
            }

            // 技能标签
            let skills = ''

            let photoUrl = $('.avater_image')[0].currentSrc

            return {
                name: name, // 姓名
                sex: sex, // 性别
                photoUrl: photoUrl, // 头像
                status: status, //在职状态
                age: age, // 年龄
                birth: '',
                workExperience: workExperience,// 工作年限
                qualifications: qualifications,// 学历
                nowCity: nowCity,// 当前所在城市
                currentSalary: currentSalary,// 当前薪资
                currentIndustry: currentIndustry,// 当前行业
                skills: skills, // 技能标签
                phone: phone, // 手机
                email: email, // 邮箱
                maritalStatus: maritalStatus, // 婚姻状况
            }
        }

        // 职业期望
        function getDesired() {
            /*
               * desiredCitys 期望城市集合
               * desiredJob 期望职位
               * desiredYearlySalary 期望年薪
               * desiredYearlySalaryRemark 年薪备注
               * industries 期望行业
               * */
            let content,
                desiredJob,
                desiredYearlySalary,
                desiredYearlySalaryRemark,
                // 行业
                industries = []
            if (elements.mind) {
                content = elements.mind.find('.tba > table >tbody > tr > td')
            } else {
                content = $()
            }
            content.find('.keys').each(function () {
                var key = $(this).text()
                if (key.match(/期望薪资/)) {
                    // 期望年薪
                    desiredYearlySalary = $(this).siblings('.txt2').text().trim()
                    desiredYearlySalaryRemark = desiredYearlySalary
                    desiredYearlySalary = $util.getSalaryMax(desiredYearlySalary)
                    if (desiredYearlySalary) {
                        if (desiredYearlySalaryRemark.indexOf('万元/年') !== -1) {
                            desiredYearlySalary = desiredYearlySalary * 10000
                        }
                        if (desiredYearlySalaryRemark.indexOf('元/月') !== -1) {
                            desiredYearlySalary = desiredYearlySalary * 12
                        }
                    }
                } else if (key.match(/地点/)) {
                    desiredCitys = $(this).siblings('.txt2').text().trim()
                } else if (key.match(/职能\/职位/)) {
                    desiredJob = $(this).siblings('.txt2').text().trim()
                } else if (key.match(/行业/)) {
                    industries = $(this).siblings('.txt2').text().trim().split(' ')
                }
            })

            let desiredJobName = $('.s-jobintension-item').eq(0).find('td table tbody tr td').eq(0).text().trim()
            let desiredCitys = $('.s-jobintension-item').eq(0).find('td table tbody tr td').eq(1).text().trim()
            let desiredSalary = $('.s-jobintension-item').eq(0).find('td table tbody tr td').eq(2).text().trim()


            return {
                desiredCitys: desiredCitys, // 期望城市集合
                desiredJob: desiredJob, // 期望职位
                desiredJobName,
                desiredSalary,
                desiredYearlySalary: desiredYearlySalary, // 期望年薪
                desiredYearlySalaryRemark: desiredYearlySalaryRemark, // 年薪备注
                industries: industries // 期望行业
            }
        }

        //  工作经历
        function getWorkInfo() {
            /*
            * companyName: 公司名,
            * companyDesc: 描述,
            * jobName: 职位,
            * department: 部门,
            * leader: 汇报对象,
            * areaType: 'sys' 所在地区,
            * areaId: 所在地区,
            * subordinateCount: 下属人数,
            * workStart: 开始时间,
            * workEnd: 结束时间,
            * yearlySalary: 年薪,
            * yearlySalaryRemark: 年薪备注,
            * duty: 职责
            * achievement: 业绩,
            * leavingReason: 离职原因
            * */
            let workEls
            if (elements.work) {
                workEls = elements.work.find('.tbb > table >tbody > tr > td > table')
            } else {
                workEls = $()
            }
            let work = []
            workEls.each(function () {
                let detail = $(this)
                let workTime = detail.find('> tbody > tr').eq(0).find('.time').text().split('-')
                let workStart = workTime[0].replace('/', '-').trim()
                let workEnd = workTime[1].replace('/', '-').trim()

                let temp = detail.find('.all').text().replace(/\s/g, '')
                let leader = temp.match(/汇报对象：(.+)\|/),
                    subordinateCount = temp.match(/下属：(\d+)人/),
                    leavingReason = temp.match(/离职原因：(.+)/),
                    area = ''

                leader = leader ? leader[1] : ''
                subordinateCount = subordinateCount ? subordinateCount[1] : 0
                leavingReason = leavingReason ? leavingReason[1] : ''

                let achievement = ''

                let duty = $util.formatStrLine(detail.find('> tbody > tr').eq(3).find('.txt1').html())

                let companyName = detail.find('> tbody > tr').eq(0).find('.rtbox .bold').text().trim()
                let companyDesc = detail.find('> tbody > tr').eq(1).find('.rtbox').text().trim()

                let jobName =  detail.find('> tbody > tr').eq(2).find('.rtbox .txt3').text().trim()
                let department = detail.find('> tbody > tr').eq(2).find('.time .txt3').text().trim()
                let salary = ''
                let yearlySalaryRemark = ''

                var item = {
                    company: companyName,// 公司名称
                    area: area || '', // 公司所在地区
                    companyDesc: companyDesc,
                    jobName: jobName, // 职位名称
                    department: department,// 所在部门
                    leader: leader,// 领导-汇报对象
                    subordinateCount: subordinateCount, //  下属人数
                    startTime: workStart, // 工作时间起
                    endTime: workEnd, // 工作时间止
                    jobSalary: salary, // 薪资待遇
                    workDescription: duty,// 职责业绩
                    achievement: achievement,
                    leavingReason: leavingReason// 离职原因
                }
                work.push(item)
            })
            return work
        }

        //  项目经历
        function getProInfo() {
            /*
             * companyName: 公司名,
             * jobName: 职位,
             * projectDesc: 项目描述,
             * projectDuty: 项目职责,
             * projectAchievement: 项目业绩,
             * projectStart: 开始时间,
             * projectEnd: 结束时间,
             * */

            let content
            if (elements.project) {
                content = elements.project.find('.tbb > table >tbody > tr > td > table')
            } else {
                content = $()
            }
            let proInfo = []
            content.each(function () {
                let detail = $(this)
                /*
                 * companyName: 公司名,
                 * jobName: 职位,
                 * description: 项目描述,
                 * content: 项目职责,
                 * projectAchievement: 项目业绩,
                 * startTime: 开始时间,
                 * endTime: 结束时间,
                 * */
                let data = {
                    companyName: '', // 公司名称
                    jobName: '', // 职位名称
                    name: '', // 项目名称
                    description: '', // 项目描述
                    content: '', // 项目职责
                    projectAchievement: '', //  项目业绩
                    startTime: null, // 项目开始时间
                    endTime: null, // 项目结束时间
                }
                data.name = detail.find('tbody > tr').eq(0).find('.rtbox .txt3').text().trim()
                let time = detail.find('tbody > tr').eq(0).find('.time').text().split('-')
                // time = $util.getStartToEndTime(time)
                data.startTime = time[0].replace('/', '-').trim()
                data.endTime = time[1].replace('/', '-').trim()

                $(this).find('.keys').each(function () {
                    let key = $(this).text()
                    if (key.match(/所属公司/)) {
                        data.companyName = $(this).siblings('.txt1').text().trim()
                    } else if (key.match(/项目描述/)) {
                        data.description = $(this).siblings('.txt1').text().trim()
                    } else if (key.match(/责任描述/)) {
                        data.content = $(this).siblings('.txt1').text().trim()
                    }
                })
                proInfo.push(data)
            })
            return proInfo
        }

        // 教育经历
        function getEducation() {
            /*
            * schoolName: 学校名称
            * majorName: 专业名称
            * startDate: 开始时间
            * endDate: 结束时间
            * qualifications: 学历
            * theEntranceExamination: 是否统招
            * */
            let content
            if (elements.eduction) {
                content = elements.eduction.find('.tba > table >tbody > tr > td > table')
            } else {
                content = $()
            }
            let education = []
            content.each(function () {
                let detail = $(this)

                // 学历和专业
                let schoolName = detail.find('> tbody > tr').eq(0).find('.rtbox .txt3').text()
                let qualifications = $util.getChildText(detail.find('> tbody > tr').eq(1).find('.tb1'))
                qualifications = qualifications.trim()
                let majorName = $util.getChildText(detail.find('> tbody > tr').eq(1).find('.tb1'))
                majorName = majorName.trim()

                let theEntranceExamination = undefined

                let time = detail.find('> tbody > tr').eq(0).find('.time').text().split('-')
                let startDate = time[0].replace('/', '-').trim()
                let endDate = time[1].replace('/', '-').trim()

                let item = {
                    school: schoolName, // 学校名称
                    majorName: majorName, // 专业名称
                    startTime: startDate, // 开始时间
                    endTime: endDate, // 结束时间
                    qualifications: qualifications, // 学位
                    theEntranceExamination: theEntranceExamination, // 是否统招
                    desc: "", // 描述
                    education: $util.getEducation(qualifications)
                }
                education.push(item)
            })
            return education
        }

        // 自我评价
        function getEvaluation() {
            if (elements.mind) {
                let evaluation = ''
                elements.mind.find('.tba > table >tbody > tr > td').each(function () {
                    let key = $(this).find('.keys').text()
                    if (key.match(/自我评价/)) {
                        evaluation = $(this).find('.txt1').last().html()
                    }
                })
                return $util.formatStrLine(evaluation)
            }
            return ''
        }

        // 其他 -- 语言能力、证书
        function getOther() {
            let other
            if (elements.skill) {
                other = elements.skill.find(' > tbody > tr > td.tbb > table > tbody > tr')
            } else {
                other = $()
            }
            // 语言
            let language = ''
            other.eq(1).find('.tbb>table>tbody>tr>td').each(function () {
                language = language
                    + $(this).find('.skill .txt3').text().trim()
                    + ' '
                    + $(this).find('.skbg .skco').text().trim()
                    + '\n'
            })
            // 证书
            let cert = []
            other.eq(3).find('> td > table >tbody > tr > td').each(function () {
                cert.push($(this).find('.rtbox').text().replace(/\s/g, ''))
            })

            cert.join(',')

            return {
                language: language,
                cert
            }
        }

        return {
            href: location.href,
            base: base,
            desired: desired,
            workList: work,
            projectList: pro,
            educationList: eduction,
            other: other,
            evaluation: evaluation,
            htmlFile: htmlFile,
            htmlFileName: '51job_' + Date.now() + '.html'
        }
    },
}

